<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_users
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_COMPONENT.'/controller.php';

/**
 * Reset controller class for Users.
 *
 * @package		Joomla.Site
 * @subpackage	com_users
 * @version		1.6
 */
class UsersControllerReset extends UsersController
{
	/**
	 * Method to request a password reset.
	 *
	 * @since	1.6
	 */
	public function request()
	{
		// Check the request token.
		JSession::checkToken('post') or jexit(JText::_('JINVALID_TOKEN'));

		$app	= JFactory::getApplication();
		$model	= $this->getModel('Reset', 'UsersModel');
		$data	= JRequest::getVar('jform', array(), 'post', 'array');

		// Submit the password reset request.
		$return	= $model->processResetRequest($data);

		// Check for a hard error.
		if ($return instanceof Exception) {
			// Get the error message to display.
			if ($app->getCfg('error_reporting')) {
				$message = $return->getMessage();
			} else {
				$message = JText::_('COM_USERS_RESET_REQUEST_ERROR');
			}

			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset'.$itemid;

			// Go back to the request form.
			$this->setRedirect(JRoute::_($route, false), $message, 'error');
			return false;
		} elseif ($return === false) {
			// The request failed.
			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset'.$itemid;

			// Go back to the request form.
			$message = JText::sprintf('COM_USERS_RESET_REQUEST_FAILED', $model->getError());
			$this->setRedirect(JRoute::_($route, false), $message, 'notice');
			return false;
		} else {
			// The request succeeded.
			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset&layout=confirm'.$itemid;

			// Proceed to step two.
			$this->setRedirect(JRoute::_($route, false));
			return true;
		}
	}

	/**
	 * Method to confirm the password request.
	 *
	 * @access	public
	 * @since	1.0
	 */
	function confirm()
	{
		// Check the request token.
		JSession::checkToken('request') or jexit(JText::_('JINVALID_TOKEN'));

		$app	= JFactory::getApplication();
		$model	= $this->getModel('Reset', 'UsersModel');
		$data	= JRequest::getVar('jform', array(), 'request', 'array');

		// Confirm the password reset request.
		$return	= $model->processResetConfirm($data);

		// Check for a hard error.
		if ($return instanceof Exception)
		{
			// Get the error message to display.
			if ($app->getCfg('error_reporting')) {
				$message = $return->getMessage();
			} else {
				$message = JText::_('COM_USERS_RESET_CONFIRM_ERROR');
			}

			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset&layout=confirm'.$itemid;

			// Go back to the confirm form.
			$this->setRedirect(JRoute::_($route, false), $message, 'error');
			return false;
		} elseif ($return === false) {
			// Confirm failed.
			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset&layout=confirm'.$itemid;

			// Go back to the confirm form.
			$message = JText::sprintf('COM_USERS_RESET_CONFIRM_FAILED', $model->getError());
			$this->setRedirect(JRoute::_($route, false), $message, 'notice');
			return false;
		} else {
			// Confirm succeeded.
			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset&layout=complete'.$itemid;

			// Proceed to step three.
			$this->setRedirect(JRoute::_($route, false));
			return true;
		}
	}

	/**
	 * Method to complete the password reset process.
	 *
	 * @since	1.6
	 */
	public function complete()
	{
		// Check for request forgeries
		JSession::checkToken('post') or jexit(JText::_('JINVALID_TOKEN'));

		$app	= JFactory::getApplication();
		$model	= $this->getModel('Reset', 'UsersModel');		$data	= JRequest::getVar('jform', array(), 'post', 'array');

		// Complete the password reset request.
		$return	= $model->processResetComplete($data);

		// Check for a hard error.
		if ($return instanceof Exception) {
			// Get the error message to display.
			if ($app->getCfg('error_reporting')) {
				$message = $return->getMessage();
			} else {
				$message = JText::_('COM_USERS_RESET_COMPLETE_ERROR');
			}

			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset&layout=complete'.$itemid;

			// Go back to the complete form.
			$this->setRedirect(JRoute::_($route, false), $message, 'error');
			return false;
		} elseif ($return === false) {
			// Complete failed.
			// Get the route to the next page.
			$itemid = UsersHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=reset&layout=complete'.$itemid;

			// Go back to the complete form.
			$message = JText::sprintf('COM_USERS_RESET_COMPLETE_FAILED', $model->getError());
			$this->setRedirect(JRoute::_($route, false), $message, 'notice');
			return false;
		} else {
			// Complete succeeded.
			// Get the route to the next page.
			$itemid = UsersHelperRoute::getLoginRoute();
			$itemid = $itemid !== null ? '&Itemid='.$itemid : '';
			$route	= 'index.php?option=com_users&view=login'.$itemid;

			// Proceed to the login form.
			$message = JText::_('COM_USERS_RESET_COMPLETE_SUCCESS');
			$this->setRedirect(JRoute::_($route, false), $message);
			return true;
		}
	}
	
	public function checkSendMail()
	{
		$db =& JFactory::getDBO(); 
		$email = JRequest::getVar('email_forget');
		$query = "SELECT * FROM #__users where email='".$email."'";
		$db->setQuery( $query );
		
        $row = $db->loadObject();
		if(count($row)>0) {
			$id_user=$row->id;
			$user = JUser::getInstance($id_user);
			$password_new		= JUserHelper::genRandomPassword(8);
			$config	= JFactory::getConfig();
			$fromname	= $config->get('fromname');
			$mailfrom	= $config->get('mailfrom');
			$sitename	= $config->get('sitename');
			$id = base64_encode($id_user);
			$pass = base64_encode($password_new);
			$subject="Nulstil adgangskode";
			$body ="Firma Navn : " . $row->username ."<br/>";
			$body .="Nyt kodeord er : " . $password_new . "<br/>
			Klik på linket <a href='".JURI::root()."index.php?option=com_users&task=reset.completepassword&iuser=".$id."&pass=".$pass."'>her</a> for at nulstille adgangskode";
			$body.= "<br/><br/>Med venlig hilsen,
						<br/><br/><a href='http://www.kokketibud.dk'><b>kokketibud.dk</b></a>
						<br/><a href='mailto:info@kokketibud.dk'>info@kokketibud.dk</a>";
			//$body.= "<br/><br/><img src='".JURI::root()."/templates/3dmaler/img/email_logo_footer.jpg'></img>";	
				
			JFactory::getMailer()->sendMail($mailfrom, $fromname, $email, $subject, $body,1);
			
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=reset&layout=complete', false));
		} else {
			
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=reset', false), 'Indtast venligst din e-mail-adresse. Denne adresse er forkert');
			
		}
	}
	public function completepassword()
	{
		$db =& JFactory::getDBO(); 
		$iuser = base64_decode(JRequest::getVar('iuser'));
		$pass = base64_decode(JRequest::getVar('pass'));
		
		$salt		= JUserHelper::genRandomPassword(8);
		$crypted	= JUserHelper::getCryptedPassword($pass, $salt);
		$password	= $crypted.':'.$salt;
        $query = "update #__users set password='".$password."' where id='".$iuser."'";   
		
        $db->setQuery( $query );
        $db->query();
		$this->setRedirect('index.php?option=com_users&view=reset&layout=complete');
		/*
		$items = getArticles(37);
		$slug = $items[0]->id.':'.$items[0]->alias;
		$catslug =$items[0]->catid ? $items[0]->catid .':'.$items[0]->category_alias : $items[0]->catid;
		require_once JPATH_SITE.'/components/com_content/helpers/route.php';
		$readmore = JRoute::_(ContentHelperRoute::getArticleRoute($slug, $catslug));
		
		$this->setRedirect($readmore);
		*/
	}
}
